package com.wisezone.springmvc.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.wisezone.springmvc.entity.Info;
import com.wisezone.springmvc.entity.User;
import com.wisezone.springmvc.service.IUserService;
import com.wisezone.springmvc.utils.FileUtils;

@Controller
@RequestMapping("/")
public class LoginController {
	@Autowired
	public IUserService userService;

	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login() {
		return "login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String loginSubmit(String username, String password, Model model) {
		User user = userService.login(username, password);
		if (user == null) {
			model.addAttribute("message", "用户名或密码出错!");
			return "login";
		}
		model.addAttribute("loginUser", "用户" + user.getUsername() + "登录成功!");
		return "main";
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String register() {
		return "register";
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String registerSubmit(@RequestParam("username") String username, String password,
			@RequestParam("headerfile") MultipartFile headerFile,HttpServletRequest request,Model model) throws IOException {
		
		//1.定义上传文件保存路径
		String db_header_uri = "/upload" + File.separator + System.currentTimeMillis() + ".jpg";// 保存数据库头像相对地址
		String header_uri = request.getSession().getServletContext().getRealPath(db_header_uri);// 文件上传保存地址
		System.out.println(header_uri);
		//2.复制MultipartFile headerFile到指定文件 (springmvc保存上传的临时文件到指定路径)
		if(headerFile != null) {
			FileUtils.createFile(header_uri); //创建保存文件
			FileCopyUtils.copy(headerFile.getInputStream(), new FileOutputStream(header_uri));
		}
		model.addAttribute("message", "文件上传成功");
		return "register";
	}
	
	/**
	 * {"code":"yes","msg","用户已经注册"}  | {"msg":"no"}
	 * @param username
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/checkuser",method=RequestMethod.POST)
	public Info checkUser(String username) {
		System.out.println("username:"+username);
		Info info = new Info();
		if("admin".equals(username)) {
			info.setCode("yes");
			info.setMsg("用户已经注册!");
		}else {
			info.setCode("no");
			info.setMsg("用户没有注册!");
		}
		return info;
	}
	@RequestMapping(value="/test_error",method=RequestMethod.GET)
	public String testError() {
		int i = 10;
		i = i/0;
		return "message";
	}
	@RequestMapping(value="/error1",method=RequestMethod.GET)
	public String testError1() {
		int[] arrays = new int[2];
		arrays[2] = 12;
		return "message";
	}
	

	
	
}
